package com.suixun.controller.credits;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.authority.controller.BaseManager;
import com.suixun.entity.form.SendGoodsForm;
import com.suixun.exception.OpServiceException;
import com.suixun.service.credits.SendGoodsService;
import com.suixun.util.Constant;

/**
 * <p>Title: SendGoodsController</p>
 * <p> 功能描述: 发券</p> 
 * @author	陈显强
 * @date	2017年12月12日上午10:54:53
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/credits/sendGoods")
public class SendGoodsController extends BaseManager {

	@Autowired
	private SendGoodsService sendGoodsService;

	/**
	 * 
	 * <p>Title: sendGoodsByRegion</p>
	 * <p> 功能描述: 按地区发券</p>
	 * @param sendGoodsForm
	 * @return
	 */
	@RequestMapping(value = "/sendByRegion.json", method = RequestMethod.POST)
	public Object sendGoodsByRegion(@RequestBody SendGoodsForm sendGoodsForm) {
		try {
			String result = sendGoodsService.sendGoodsByRegion(sendGoodsForm, getUserId());
			if (result == null) {
				return success();
			} else {
				return fail(result);
			}
		} catch (Exception e) {
			if(e instanceof OpServiceException) {
				return fail(e.getMessage());
			}
			logger("SendGoodsController sendGoodsByRegion:" + e.getMessage(), e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}

	/**
	 * 
	 * <p>Title: sendGoodsByPeople</p>
	 * <p> 功能描述: 指定用户发券</p>
	 * @param sendGoodsForm
	 * @return
	 */
	@RequestMapping(value = "/sendByPeople.json", method = RequestMethod.POST)
	public Object sendGoodsByPeople(@RequestBody SendGoodsForm sendGoodsForm) {
		try {
			String result = sendGoodsService.sendGoodsByPeoples(sendGoodsForm, getUserId());
			if (result == null) {
				return success();
			} else {
				return fail(result);
			}
		} catch (Exception e) {
			if(e instanceof OpServiceException) {
				return fail(e.getMessage());
			}
			logger("SendGoodsController sendGoodsByPeople:" + e.getMessage(), e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	@RequestMapping(value = "/pageSendGoodsRecord.json")
	public Object pageSendGoodsRecord(Integer page, Integer size, String startTime, String endTime, String content) {
		try {
			if(page == null) {
				page = 1;
			}
			if(size == null) {
				size = 15;
			}
			return success(sendGoodsService.pageSendGoodsRecord(page, size, startTime, endTime, content));
		} catch (Exception e) {
			logger("SendGoodsController PageSendGoodsRecord:" + e.getMessage(), e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	@RequestMapping(value = "/getSendGoodsRecord.json")
	public Object getSendGoodsRecord(Integer recordId) {
		try {
			return success(sendGoodsService.getSendGoodsRecord(recordId));
		} catch (Exception e) {
			logger("SendGoodsController getSendGoodsRecord:" + e.getMessage(), e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
}
